package 力扣日常刷题.木22二月.第07天213;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author 帅小伙
 * @date 2022/2/13
 * @description
 */
public class Demo54JZ数据流中的中位数 {

    class MedianFinder {
        Queue<Integer> A,B;
        public MedianFinder() {
            A = new PriorityQueue<>();
            B = new PriorityQueue<>((x,y)->(y - x));//大顶堆
        }

        public void addNum(int num) {
            if(A.size() != B.size()) {
                A.add(num);
                B.add(A.poll());
            } else {
                B.add(num);
                A.add(B.poll());
            }
        }

        public double findMedian() {
            return A.size() != B.size() ? A.peek() : (A.peek() + B.peek())/2.0;
        }
    }

}
